<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Error;


class ErrorController extends Controller
{
    public function __construct()
    {
        view()->share('menu', 'count');
    }
    //
    /**
     * 错误记录
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/7/12
     * @access public|private|protected
     * @param  mixed    name    comment
     * @param  int    name    comment
     * @param  string    name    comment
     * @param  bool       name    comment
     * @param  array   name    comment
     * @return void|int|string|boolean|array        comment
     */
    public function index(Request $request){
        $type = $request->get('type');
        if (!empty($type))
            $this->log_del($type);
        $data = Error::orderBy('id','desc')->paginate(10);
        $list = [];
        foreach ($data as $k=>$v){
            $list[$k]['id'] = $v->id;
            if ($v->uid>0){
                $list[$k]['user'] = User::where('id',$v->uid)->value('account');
            }else{
                $list[$k]['user'] = '-';
            }
            $list[$k]['OS_Version'] = $v->OS_Version;
            $list[$k]['SDK_Version'] = $v->SDK_Version;
            $list[$k]['vendor'] = $v->vendor;
            $list[$k]['APP_Version'] = $v->APP_Version;
            $list[$k]['ex_info'] = substr($v->ex_info, 0, 20);
            $list[$k]['type'] = $v->type;
            $list[$k]['time'] = date("Y-m-d h:i:s", $v->time);
        }
        return view('admin/error/index', compact('list','data'));
    }

    public function log_info(Request $request)
    {
        $id = $request->get('id');
        $infos = Error::where('id',$id)->first();
        if ($infos->uid>0){
            $infos->user = User::where('id',$infos->uid)->value('account');
        }else{
            $infos->user = '-';
        }
        $infos->time = date("Y-m-d h:i:s", $infos->time);
        return view('admin/error/log_info', compact('infos'));
    }

    public function log_del($type){
        if ($type==1){
            $time = time()-(2*24*60*60);
            Error::where('time','<',$time)->delete();
        }else{
            Error::where('id','>',0)->delete();
        }
        return back()->with(['msg'=>'清理成功','status'=>1,'msg_url'=>'error_list']);

    }
}
